<template>
  <el-dialog :close-on-click-modal="false" v-drag :title="record.rule_id ? $t('编辑') : $t('添加')" append-to-body :v-if="dialogVisible" :visible.sync="dialogVisible" width="510px" top="5vh" @close="close">
    <div class="dialogContentBoxMaxHeight90">
      <el-form ref="form" :inline="true" :model="record" label-width="140px" :rules="rules" label-position="left" style="margin: 0 30px">
        <el-form-item :label="$t('协议名称')" class="opcForm" prop="proto">
          <span style="color: #999">{{ rule[record.proto] }}</span>
        </el-form-item>
        <el-form-item :label="$t('类型')" prop="operate_type">
          <el-radio-group v-model="record.operate_type" :disabled="record.rule_id ? true : false" @change="operateTypeChange">
            <el-radio :label="0" class="deeepfilts">{{$t('深度过滤')}}</el-radio>
            <el-radio :label="1" class="operate">{{$t('关联操作')}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('动作')" class="opcForm" prop="action">
          <el-select v-model="record.action" ref="recordAction" class="opcLength" size="small" filterable>
            <el-option v-for="(value, key) in actionList" :key="key" :label="$t(value)" :value="key">
            </el-option>
          </el-select>
        </el-form-item>
        <div v-if="record.operate_type === 0">
          <el-form-item :label="$t('功能码')" prop="tag_type">
            <el-radio-group v-model="record.tag_type" @change="tagTypeChange">
              <el-radio label="add" class="add">{{$t('添加站点')}}</el-radio>
              <el-radio label="write" class="write">{{$t('写值')}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item :label="$t('匹配范围')" class="opcForm" prop="accept_range">
            <el-radio-group v-model="record.accept_range" @change="tagTypeChange">
              <el-radio label="all" class="all">{{$t('所有')}}</el-radio>
              <el-radio label="single" class="single">{{$t('单个')}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <div v-if="record.accept_range === 'single'">
            <el-form-item :label="$t('点名')" class="opcForm" prop="tag_name" key="tag_name">
              <el-input v-emoji v-model="record.tag_name" ref="names" :maxlength="128" class="opcLength" size="small"></el-input>
            </el-form-item>
            <el-form-item :label="$t('数据类型')" class="opcForm" v-if="record.tag_type === 'write'" prop="value_type">
              <el-select v-model="record.value_type" ref="valueType" class="opcLength" size="small" filterable @change="valueRangeChange">
                <el-option v-for="(value, key) in typeList" :key="key" :label="$t(value)" :value="key">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item :label="$t('取值范围')" prop="register_value_range_select" v-if="record.tag_type === 'write' && record.value_type === 'BOOL'" key=" register_value_range_select">
              <el-select v-model="record.register_value_range_select" ref="rangeSelects" class="opcLength" @change="valueRangeChange" size="small" filterable>
                <el-option v-for="(value, key) in rangeList" :key="key" :label="$t(value)" :value="key">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item :label="$t('取值范围')" prop="value_range_start" style="margin-right: 0" v-if="record.tag_type === 'write' &&
              !showSelectList.includes(record.value_type)
              " key="value_range_start">
              <el-input v-emoji v-model="record.value_range_start" ref="starts" :maxlength="100" @change="valueRangeChange" style="width: 117px" size="small"></el-input>
            </el-form-item>
            <el-form-item label="" v-if="record.tag_type === 'write' &&
              !showSelectList.includes(record.value_type)
              " style="margin-right: 0">~</el-form-item>
            <el-form-item label-width="0" v-if="record.tag_type === 'write' &&
              !showSelectList.includes(record.value_type)
              " prop="value_range_end" key="value_range_end">
              <el-input v-emoji v-model="record.value_range_end" ref="ends" :maxlength="100" @change="valueRangeChange" style="width: 117px" size="small"></el-input>
            </el-form-item>
            <el-form-item :label="$t('取值范围')" prop="value_range_start_string" style="margin-right: 0" v-if="record.tag_type === 'write' && record.value_type === 'STRING'
              " key="value_range_start_string">
              <el-input v-emoji v-model="record.value_range_start_string" ref="opcLength" :maxlength="128" @change="valueRangeChange" class="opcLength" size="small"></el-input>
            </el-form-item>
          </div>
        </div>
        <div v-if="record.operate_type === 1">
          <el-form-item :label="$t('写操作点名')" class="opcForm" prop="be_relate_tag_name" key="be_relate_tag_name">
            <el-input v-emoji v-model="record.be_relate_tag_name" :maxlength="128" ref="relate" class="opcLength" size="small"></el-input>
          </el-form-item>
          <el-form-item :label="$t('关联写操作点名')" class="opcForm" prop="relate_tag_name" key="relate_tag_name">
            <el-input v-emoji v-model="record.relate_tag_name" ref="names" :maxlength="128" class="opcLength" size="small"></el-input>
          </el-form-item>
          <el-form-item :label="$t('关联写操作数据类型')" class="opcForm" prop="relate_value_type">
            <el-select v-model="record.relate_value_type" ref="rangeSelect" class="opcLength" size="small" filterable @change="registerRangeChange">
              <el-option v-for="(value, key) in typeList" :key="key" :label="$t(value)" :value="key">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('关联写操作安全值范围')" prop="register_value_range_select_write" v-if="record.relate_value_type === 'BOOL'" key="register_value_range_select_write">
            <el-select v-model="record.register_value_range_select_write" ref="selectWrite" class="opcLength" @change="registerRangeChange" size="small" filterable>
              <el-option v-for="(value, key) in rangeList" :key="key" :label="$t(value)" :value="key">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('关联写操作安全值范围')" prop="value_range_start_write" style="margin-right: 0" v-if="!showSelectList.includes(record.relate_value_type)" key="value_range_start_write">
            <el-input v-emoji v-model="record.value_range_start_write" ref="start" :maxlength="100" @change="registerRangeChange" style="width: 117px" size="small"></el-input>
          </el-form-item>
          <el-form-item label="" v-if="!showSelectList.includes(record.relate_value_type)" style="margin-right: 0">~</el-form-item>
          <el-form-item label-width="0" v-if="!showSelectList.includes(record.relate_value_type)" prop="value_range_end_write" key="value_range_end_write">
            <el-input v-emoji v-model="record.value_range_end_write" ref="end" :maxlength="100" @change="registerRangeChange" style="width: 117px" size="small"></el-input>
          </el-form-item>
          <el-form-item :label="$t('关联写操作安全值范围')" prop="value_range_string_write" style="margin-right: 0" v-if="record.relate_value_type === 'STRING'" key="value_range_string_write">
            <el-input v-emoji v-model="record.value_range_string_write" :maxlength="128" @change="registerRangeChange" class="opcLength" size="small"></el-input>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="innerAddSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="innerAddClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import ruleArray from '../../ruleArray'
import { inputClass, selectClass } from '@/utils/input-class.js'
export default {
  name: 'OpcDialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else {
        callback()
      }
    }
    var validateValueRangeStart = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (isNaN(Number(value))) {
        callback(new Error(this.$t('只能输入数字')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
        // } else if (Number(value) < -32768 || Number(value) > 32767) {
        //   callback(new Error(this.$t('输入范围-32768~32767')))
      } else if (this.record.value_type === 'STRING' && value.length > 128) {
        callback(new Error(this.$t('长度不能超过128')))
      } else if (
        this.record.value_range_start &&
        this.record.value_range_end &&
        Number(this.record.value_range_start) >
        Number(this.record.value_range_end)
      ) {
        callback(new Error(this.$t('不能大于最大值')))
      } else {
        callback()
      }
    }
    var validateValueRangeEnd = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (isNaN(Number(value))) {
        callback(new Error(this.$t('只能输入数字')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
        // } else if (Number(value) < -32768 || Number(value) > 32767) {
        //   callback(new Error(this.$t('输入范围-32768~32767')))
      } else if (this.record.value_type === 'STRING' && value.length > 128) {
        callback(new Error(this.$t('长度不能超过128')))
      } else if (
        this.record.value_range_start &&
        this.record.value_range_end &&
        Number(this.record.value_range_start) >
        Number(this.record.value_range_end)
      ) {
        callback(new Error(this.$t('不能小于最小值')))
      } else {
        callback()
      }
    }
    var validateValueRangeWriteStart = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (isNaN(Number(value))) {
        callback(new Error(this.$t('只能输入数字')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
        // } else if (Number(value) < 0 || Number(value) > 65535) {
        //   callback(new Error(this.$t('范围0~65535')))
      } else if (
        this.record.relate_value_type === 'STRING' && value.length > 128
      ) {
        callback(new Error(this.$t('长度不能超过128')))
      } else if (
        this.record.value_range_start_write &&
        this.record.value_range_end_write &&
        Number(this.record.value_range_start_write) >
        Number(this.record.value_range_end_write)
      ) {
        callback(new Error(this.$t('不能大于最大值')))
      } else {
        callback()
      }
    }
    var validateValueRangeWriteEnd = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (isNaN(Number(value))) {
        callback(new Error(this.$t('只能输入数字')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
        // } else if (Number(value) < 0 || Number(value) > 65535) {
        //   callback(new Error(this.$t('范围0~65535')))
      } else if (
        this.record.relate_value_type === 'STRING' &&
        value.length > 128
      ) {
        callback(new Error(this.$t('长度不能超过128')))
      } else if (
        this.record.value_range_start_write &&
        this.record.value_range_end_write &&
        Number(this.record.value_range_start_write) >
        Number(this.record.value_range_end_write)
      ) {
        callback(new Error(this.$t('不能小于最小值')))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        accept_range: 'all',
        action: 'accept',
        match_direction: '-',
        proto: 'OPCDA',
        operate_type: 0,
        be_relate_tag_name: '',
        relate_tag_name: '',
        relate_value_type: 'BOOL',
        register_value_range_select_write: '0',
        value_range_start_write: '',
        value_range_end_write: '',
        value_range_string_write: '',
        relate_value_range: '',
        rule_id: null,
        tag_name: '0',
        tag_type: 'add',
        value_range: '',
        value_type: 'BOOL',
        // -----
        register_value_range_select: '0',
        value_range_start_string: '',
        value_range_end: '',
        value_range_start: ''
      },
      recordCopy: {
        accept_range: 'all',
        action: 'accept',
        match_direction: '-',
        proto: 'OPCDA',
        operate_type: 0,
        be_relate_tag_name: '',
        relate_tag_name: '',
        relate_value_type: 'BOOL',
        register_value_range_select_write: '0',
        value_range_start_write: '',
        value_range_end_write: '',
        value_range_string_write: '',
        relate_value_range: '',
        rule_id: null,
        tag_name: '0',
        tag_type: 'add',
        value_range: '',
        value_type: 'BOOL',
        // -----
        register_value_range_select: '0',
        value_range_start_string: '',
        value_range_end: '',
        value_range_start: ''
      },
      rule: ruleArray.ruleShow,
      typeList: ruleArray.opcValueTypeList,
      actionList: { accept: '允许', alert: '告警' },
      showSelectList: ['BOOL', 'STRING'],
      rangeList: ruleArray.rangeList,
      rules: {
        tag_name: [ { required: false, validator: validateName, trigger: 'blur' } ],
        be_relate_tag_name: [ { required: false, validator: validateName, trigger: 'blur' } ],
        relate_tag_name: [ { required: false, validator: validateName, trigger: 'blur' } ],
        value_range_start: [ { required: false, validator: validateValueRangeStart, trigger: 'blur' } ],
        value_range_end: [ { required: false, validator: validateValueRangeEnd, trigger: 'blur' } ],
        value_range_start_string: [ { required: false, validator: validateName, trigger: 'blur' } ],
        value_range_start_write: [ { required: false, validator: validateValueRangeWriteStart, trigger: 'blur' } ],
        value_range_end_write: [ { required: false, validator: validateValueRangeWriteEnd, trigger: 'blur' } ],
        value_range_string_write: [ { required: false, validator: validateName, trigger: 'blur' } ]
      }
    }
  },
  computed: {
    change () {
      const {
        operate_type,
        accept_range,
        tag_type,
        relate_value_type,
        value_type
      } = this.record
      return {
        operate_type,
        accept_range,
        tag_type,
        relate_value_type,
        value_type
      }
    }
  },
  watch: {
    change (val) {
      if (val.operate_type === 0 && val.accept_range === 'single') {
        this.$nextTick(() => {
          const data = ['names']
          inputClass(this.$refs, data)
        })
        if (val.tag_type === 'write') {
          this.$nextTick(() => {
            const selectData = ['valueType']
            selectClass(this.$refs, selectData)
          })
          if (val.value_type === 'BOOL') {
            this.$nextTick(() => {
              selectClass(this.$refs, ['rangeSelects'])
            })
          }
          if (!this.showSelectList.includes(val.value_type)) {
            this.$nextTick(() => {
              const data = ['ends', 'starts']
              inputClass(this.$refs, data)
            })
          }
        }
      } else if (val.operate_type === 1) {
        this.$nextTick(() => {
          const data = ['names', 'relate']
          const selectData = ['rangeSelect']
          selectClass(this.$refs, selectData)
          inputClass(this.$refs, data)
        })
      }
      if (val.tag_type === 'write' && val.value_type === 'STRING') {
        this.$nextTick(() => {
          inputClass(this.$refs, ['opcLength'])
        })
      }
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        const selectData = ['recordAction']
        selectClass(this.$refs, selectData)
      })
      if (data) {
        this.record = Object.assign(this.record, data)
        // 根据类型区分深度过滤和关联操作
        if (this.record.operate_type === 0) {
          if (this.record.value_type === 'BOOL') {
            this.record.register_value_range_select = this.record.value_range.split('~')[0] || '0'
          } else if (this.record.value_type === 'STRING') {
            this.record.value_range_start_string = this.record.value_range
          } else if (!this.showSelectList.includes(this.record.value_type)) {
            this.record.value_range_start = this.record.value_range.split('~')[0] || ''
            this.record.value_range_end = this.record.value_range.split('~')[1] || ''
          }
        } else {
          if (this.record.relate_value_type === 'BOOL') {
            this.record.register_value_range_select_write = this.record.relate_value_range.split('~')[0] || '0'
          } else if (this.record.relate_value_type === 'STRING') {
            this.record.value_range_string_write = this.record.relate_value_range
          } else if (!this.showSelectList.includes(this.record.relate_value_type)) {
            this.record.value_range_start_write = this.record.relate_value_range.split('~')[0] || ''
            this.record.value_range_end_write = this.record.relate_value_range.split('~')[1] || ''
          }
        }
      }
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          var formData = JSON.parse(JSON.stringify(this.record))
          this.$emit('addConfigRuleMap', formData)
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.$emit('childrenClose')
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    },
    valueRangeChange () {
      if (this.record.value_type === 'BOOL') {
        this.record.value_range =
          this.record.register_value_range_select + '~0'
      } else if (this.record.value_type === 'STRING') {
        this.record.value_range = this.record.value_range_start_string
      } else if (!this.showSelectList.includes(this.record.value_type)) {
        this.record.value_range = this.record.value_range_start + '~' + this.record.value_range_end
      }
    },
    tagTypeChange (data) {
      this.record.value_type = 'BOOL'
      this.record.tag_name = '0'
      this.valueRangeChange()
    },
    isInteger (value) {
      return (
        !isNaN(value) &&
        parseInt(value).toString().length === String(Number(value)).length
      )
    },
    operateTypeChange (data) {
      this.record.proto = data === 0 ? 'OPCDA' : 'OPCDA_CONNECT'
    },
    registerRangeChange () {
      if (this.record.relate_value_type === 'BOOL') {
        this.record.relate_value_range =
          this.record.register_value_range_select_write + '~0'
      } else if (this.record.relate_value_type === 'STRING') {
        this.record.relate_value_range = this.record.value_range_string_write
      } else if (!this.showSelectList.includes(this.record.relate_value_type)) {
        this.record.relate_value_range =
          this.record.value_range_start_write +
          '~' +
          this.record.value_range_end_write
      }
    }
  }
}
</script>
<style>
.opcLength {
  width: 250px;
}
</style>
